// pages/onlineUnpaid/onlineUnpaid.js
const app = getApp()
import api from '../../utils/api.js'
let t;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderDetailInfo: {},

    h: 0,
    m: 0,
    s: 0,

    countdown: '',
    canPay: true,
    // order_identifier:null
  },

  goToPay() {
    if (this.data.canPay) {
      console.log(this.data.orderDetailInfo)
      let order_identifier = this.data.orderDetailInfo.order_identifier
      // this.setData({
      //   order_identifier: this.data.orderDetailInfo.exam_order_id
      // })
      // console.log(this.data.order_identifier)
      // let obj = {}
      // obj.order_identifier = this.data.orderDetailInfo.exam_order_id
      // api.POST('/applet/exam_order/checkExamPermit', obj, res => {
      //   // 准考证
      //   console.log(res.data)
      //   app.globalData.card = res.data
      // })

      // api.POST('/applet/ContactService/detail', {}, res => {
      //   app.globalData.QR = res.data.exam_card_url
      // })
      this.pay(order_identifier)
    } else if (!this.data.canPay) {
      wx.showToast({
        title: '抱歉，已过报名时间',
        icon: 'none'
      })
    }

  },

  pay(id) {
    const that = this;
    api.POST('applet/customer/jsApiPay', {
      out_trade_no: id,
      body: "123",
      attach: "123"
    }, res => {
      console.log(res)
      that.requestPayment(res.data)
    })
  },

  requestPayment(payModel) {
    //  获取微信支付的数据
    let that = this;
    wx.requestPayment({
      'timeStamp': payModel.timeStamp,
      'nonceStr': payModel.nonceStr,
      'package': payModel.package,
      'paySign': payModel.paySign,
      'signType': "HMAC-SHA256",
      'success': function (res) {
        console.log('支付成功')
        setTimeout(function () {
          wx.navigateTo({
            url: '/pages/onlineApplySuccess/onlineApplySuccess?pid=' + that.data.orderDetailInfo.order_identifier,
          })
        }, 500)
        // that.getInit();
      },
      'fail': function (res) {
        console.log('支付失败');
        // wx.showModal({
        //   title: '提示',
        //   content: '支付失败',
        //   showCancel: false,
        //   success: function (res) {
        //     if (res.confirm) {
        //       wx.reLaunch({
        //         url: '../order/order?oid=' + that.data.oid,
        //       })
        //     }
        //   }
        // });
      }
    })
  },
  countdown() {
    // const that=this;
    let now = new Date().getTime()
    let stop=this.data.stop;
    if (now >= stop) {
      this.setData({
        countdown: '已过报名结束时间',
        canPay: false
      })
      clearTimeout(t)
      return
    } else{
      let Milliseconds = stop - now

      let hour = Math.floor(Milliseconds / 1000 / 60 / 60);
      let h = hour < 10 ? ('0' + hour) : hour

      let minute = Math.floor(Milliseconds / 1000 / 60 % 60);
      let m = minute < 10 ? ('0' + minute) : minute

      let second = Math.floor(Milliseconds / 1000 % 60)
      let s = second < 10 ? ('0' + second) : second

      this.setData({
        h,
        m,
        s
      })
      t=setTimeout(this.countdown, 1000)
    }
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    api.POST('/applet/exam_order/detail', {
      exam_order_id: options.oid
    }, res => {
      console.log(res)
      this.setData({
        orderDetailInfo: res.data,
        stop: new Date(res.data.en_stop_time).getTime()
      })
   
      this.countdown()
    })
    
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearTimeout(t)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})